package com.example.interceptor;



import com.example.entity.IpAddressRecorder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class IpAddressInterceptor implements HandlerInterceptor {

    @Autowired
    private IpAddressRecorder ipAddressRecorder;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String ipAddress = request.getRemoteAddr();
        ipAddressRecorder.addIpAddress(ipAddress);
        return true;
    }


}
